﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace MvcControlsToolkit
{
    public class AutoComplete: Control
    {
        public bool IsAutoFocus
        {
            get
            {
                if (Attributes.ContainsKey("autoFocus"))
                    return bool.Parse(Attributes["autoFocus"]);
                else return false;
            }
            set { Attributes["autoFocus"] = value.ToString().ToLower(); }
        }

        public int Delay
        {
            get
            {
                if (Attributes.ContainsKey("delay"))
                    return int.Parse(Attributes["delay"]);
                else return 1;
            }
            set { Attributes["delay"] = value.ToString().ToLower(); }
        }

        public string RequestType
        {
            get { return GetString("requestType"); }
            set { SetString("requestType", value); }
        }

        public int MinLength
        {
            get
            {
                if (Attributes.ContainsKey("minLength"))
                    return int.Parse(Attributes["minLength"]);
                else return 1;
            }
            set { Attributes["minLength"] = value.ToString().ToLower(); }
        }

        public object Position 
        { 
            get
            {
                if (Attributes.ContainsKey("position"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<string[]>(Attributes["position"]);
                    return result;
                }
                else return null;
            } 
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["position"] = result.ToLower();
            } 
        }

        //private ContentType ContentType { get; set; }

        private string Url
        {
            //get { return Attributes["source"]; }
            //set { Attributes["source"] = value; }
            get; set; }

        public object Source
        {
            get
            {
                if (Attributes.ContainsKey("source"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<string[]>(Attributes["source"]);
                    return result;
                }
                else return null;
            }
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["source"] = result.ToLower();
                //ContentType = ContentType.Static;
            }
        }

        public AutoComplete(string name):base(name)
        {
            //ContentType = ContentType.Static;
        }

        //public AutoComplete Bind(object source)
        //{
        //    Source = source;
        //    return this;
        //}

        //public AutoComplete Bind(string url)
        //{
        //    Url = url;
        //    return this;
        //}

        public override string GetJScript()
        {
            var result = new StringBuilder();

            foreach (string key in Attributes.Keys)
            {
                result.AppendFormat(", {0}: {1}", key, Attributes[key]);
            }

            if (result.Length > 0)
                result.Remove(0, 1);

            result.Append("});");

            result.Insert(0, "var " + Name + " = $(\"#" + Name + "\").autocomplete({");

            return result.ToString();
        }

        public override string GetHtml()
        {
            return String.Format("<input id=\"{0}\" />", Name);
        }
    }
}
